Brief Full Advanced Search Search Tips
|  |  | To access the contents, click the chapter and section titles. Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6! (Publisher: The Coriolis Group) Author(s): Peter G. Aitken ISBN: 1576102815 Publication Date: 08/01/98 Search this book:
Previous | Table of Contents | Next
Variable names should follow one other guideline. While it may not be an absolute requirement, it is nonetheless important enough to merit the designation of a rule: Use descriptive variable names. This means simply that the name of a variable should describe the data it holds. For example, if your program deals with the population genetics of bats, and you need a variable to hold the number of bats, you should use a name such as Bat_Count or NumberOfBats. You could legally name the variable XY, J89, or BillClinton, and Visual Basic wouldn’t care. But when working on the program later, you’ll find that descriptive variable names make a big difference in the readability of your code. It’s a good habit to cultivate right from the get-go. Some programmers believe that descriptive variable naming should be taken a step further so that the name of each variable identifies not only its contents but its type. This is typically done with a type of prefix called Hungarian notation. Using this prefix, the name of an integer variable might be preceded with an i for integer. For your bat count, therefore, you would have a variable named iNumberOfBats. The idea behind Hungarian notation is the ability to discern the variable type immediately whenever you see a variable name in code. In my opinion, there is no need for the inconvenience of Hungarian notation in Visual Basic. The Visual Basic code editor already has a feature that makes it a cinch to determine the type of any variable. Simply right-click on any variable name, then select Quick Info from the pop-up menu. Visual Basic will display a small window with the variable type indicated. You can get the same result by placing the editing cursor on the variable and pressing Ctrl+I. With such easy access to variable types, why bother with the awkward and inconvenient variable names created by Hungarian notation? TIP: Standardizing Variable Naming Conventions Visual Basic does not care one whit about the case of variable names. As far as Basic is concerned, Count, count, and COUNT all reference the same variable. For the sake of code readability, I suggest that you adopt a standard variable naming scheme and stick to it. My method is to use a combination of upper- and lowercase letters for variable names. This approach makes the names easier to read, particularly when a variable name has been constructed by combining two or more words. For example, I think that MaxDataCount is much easier to read than maxdatacount. As I’ll soon explain, I reserve names that are all uppercase for constants. Visual Basic’s code editor makes it easy to be consistent with your use of capitalization. Once you’ve entered the first instance of a variable name, which is usually in a Dim statement, Visual Basic automatically corrects future instances of the name to match the original capitalization. How is a variable’s type assigned? You have two choices. You can explicitly declare the variable in a Dim statement, or you can use a type declaration character at the end of the variable name. An explicit declaration takes the following general form:
    Dim name As type
Here’s a specific example:
    Dim X As Integer, Y As Single, Z As Currency
Each variable name is listed, followed by the As keyword and the type name. You can have one or more variables in a single Dim statement, and you can have as many Dim statements as you need, each on its own line. The use of type declaration characters makes Dim statements unnecessary. I’ll explain in a minute why I think that the use of type declaration characters is a bad idea, but you do need to know about them. To use the type declaration characters, simply place the character corresponding to the desired variable type at the end of the variable name. The characters are shown in Table 4.2. At this point, because no Dim statement is necessary, you simply use the variable in code as needed. For example, if you need a type Integer variable to hold the value 100, you would write as follows:
    Count% = 100
In other words, you use the variables only when and where they are needed—you don’t have to declare them first. This all sounds quite convenient, but now I’ll explain why it’s a bad idea. To Declare Or Not To Declare The answer to this question—to declare or not—is an easy one. Always declare your variables. In fact, you can tell Visual Basic to require variable declaration. Select Options from the Tools menu, click on the Editor tab, and turn on the Require Variable Declaration option. When this option is active, Visual Basic will report a Variable Not Defined error if you try to use a variable that has not been declared in a Dim statement. Table 4.2 Type declaration characters.
Type | Declaration Character
--- | ---
Integer | %
Long | &
Single | !
Double | #
Currency | @
Note: When the Require Variable Declaration option is on, Visual Basic inserts the Option Explicit statement in your code. Why am I so adamant about always declaring your variables? Why wouldn’t the use of type declaration characters be easier? Though it may appear easier at first, it always seems to lead to problems. Here’s why. Let’s say you’ve created a variable, but at some point in the code the name is misspelled. Without Option Explicit active, Visual Basic will simply create a new variable using the misspelled name (as far as Visual Basic is concerned, you are creating a new variable). New variables are automatically given the value of zero. Because the program is using this new variable instead of the one it should be using, you are almost sure to get program errors that are extremely hard to track down. If Option Explicit were active, however, Visual Basic would immediately flag the misspelled variable name as an undeclared variable. The problem would never arise. But this isn’t the only reason to declare your variables. Some of Visual Basic’s variable types—Byte, Boolean, and Date (I’ll get to the last two soon)—do not have a type declaration character associated with them. If you want to create a variable with one of those types, you must use a Dim statement. If you are not requiring variable declarations, you’ll find yourself in a situation where some of your program’s variables are declared in Dim statements, while others are not. This is a recipe for confusion, and any seasoned programmer knows potential confusion should be nipped in the bud.
Previous | Table of Contents | Next

| Products | Contact Us | About Us |